home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 363 < prev    next >
Internet Message Format  |  1996-08-06  |  5KB

  1. Path: solon.com!not-for-mail
  2. From: msb@sq.com
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Followup-To: comp.std.c
  6. Date: 23 Feb 1996 07:13:05 -0600
  7. Organization: SoftQuad Inc., Toronto, Canada
  8. Sender: clc@solutions.solon.com
  9. Approved: clc@solutions.solon.com
  10. Message-ID: <4gkeh1$rcp@solutions.solon.com>
  11. References: <4fstj7$2l6@solutions.solon.com> <4g28hr$qko@solutions.solon.com> <4g54pv$ajt@solutions.solon.com> <4gfe6i$33s@solutions.solon.com>
  12. NNTP-Posting-Host: solutions.solon.com
  13.  
  14. I wrote:
  15. >>>> Thus even in ANSI C, if this function is called this way:
  16. >>>>    extern short p, q, r, test(short,short);
  17. >>>>    r = test(p,q);
  18. >>>> the values from p and q are still converted from short to int and back
  19. >>>> to short.  [but in the caller, where this is trivially optimizable-out]
  20.  
  21. And when challenged, justified this by citing:
  22. >> ... 6.2.1.1/3.2.1.1, the passage which defines the integral
  23. >> promotions.  It states that the promotions apply
  24. >> #  in an expression wherever an int or unsigned int may be used.
  25.  
  26. This interpretation was objected to in the Referenced article by
  27. Pete Becker (pete@borland.com):
  28.  
  29. | there is a footnote to this section that says
  30. |
  31. |    The integral promotions are applied only as part of the usual
  32. |    arithmetic conversions, to certain argument expressions, to the
  33. |    operands of the unary +, -, and ~ operators, and to both operands
  34. |    of the shift operators, AS SPECIFIED BY THEIR RESPECTIVE SECTIONS.
  35. |    [emphasis added]
  36. |
  37. | That is, this section does not tell you what must be done every time
  38. | you encounter something that could be promoted to int.  It tells you
  39. | what the term "integral promotions" means when it is used in other
  40. | sections...  In particular, it tells you what "integral promotions"
  41. | means in the context of the "usual arithmetic conversions", which is
  42. | where it is most often encountered.
  43.  
  44. And in article <4gfavt$21b@solutions.solon.com>, in another branch of
  45. the thread, by Norman Diamond (diamond@jrdv04.enet.dec-j.co.jp):
  46.  
  47. > 6.2/3.2 begins with the sentence:  "Several operators convert values
  48. > from one type to another automatically." ...  The entire section is
  49. > irrelevant to other contexts, such as those in which operators do not
  50. > convert values automatically...
  51.  
  52. It is clear both from the footnote and the abundant references to
  53. integral promotions throughout 6.3.*/3.3.* that Pete and Norman have
  54. correctly interpreted the intent of the standard, so I was wrong to
  55. that intent.  In my defense, I was fooled by the very explicit statement
  56. that I cited in 6.2.1.1/3.2.1.1 -- and by similar language in K&R1, mem-
  57. orized long ago -- into not looking for material that might contradict it.
  58.  
  59. And in fact, as to what the standard actually requires, I maintain that
  60. I was right and Norman and Pete wrong.  But it doesn't matter anyway,
  61. because both interpretations always yield the same result!
  62.  
  63. First off, footnotes are not part of the standard, so if they contradict
  64. the text of the standard, they are supposed to be ignored.  Preferably,
  65. in that case someone files a Defect Report and eventually the conflict
  66. is resolved -- as Tanmoy Bhattacharya said in yet another branch of
  67. the thread, moving the footnote into the main text would make sense.
  68.  
  69. Second, 6.2/3.2, in common with other sections of similar type, is best
  70. read as a synopsis of the primary purpose of the sections that follow.
  71. It is not stated as explicitly taking precedence over other material
  72. such as 6.2.1.1/3.2.1.1; contrast this with the way that 7.1.7/4.1.6
  73. does explicitly take precedence over the sections following it.
  74.  
  75. Also contrast 6.2.1.1/3.2.1.1, which defines the integral promotions
  76. and states where they apply, with 6.2.1.5/3.2.1.5, which defines the
  77. usual arithmetic conversions but does NOT state where they apply.
  78. That is left to later sections in 6.3.*/3.3.*.
  79.  
  80. It is true that various sections in 6.3.*/3.3.* refer explicitly to
  81. integral promotions occurring; these are redundant, since the values
  82. in question would already have undergone the integral promotions
  83. automatically (and a second integral promotion causes no change).
  84. They do, as I said above, confirm that the intent was otherwise than
  85. as I said previously.
  86.  
  87. Finally, there is the matter of sizeof.  If c is a char, and values
  88. of type char are converted automatically to int, does it follow that
  89. sizeof c should produce sizeof(int)?  No, it doesn't.  The reason
  90. is that the operand of sizeof is defined as not being evaluated.
  91. If it isn't evaluated, there's no value involved, and it's only
  92. *values* ("rvalues" if you prefer) that undergo the integral promotions.
  93.  
  94. But none of this matters, because none of it ever causes an expression
  95. to produce a different result.  Under the "as if" rule, if redundant
  96. type conversions are required, they're not required to be *implemented*.
  97.  
  98. Followups directed to comp.std.c.
  99. -- 
  100. Mark Brader, msb@sq.com, SoftQuad Inc., Toronto
  101. #define MSB(type)       (~(((unsigned type)-1)>>1))
  102.  
  103. My text in this article is in the public domain.
  104.